package com.boco.modules.fdoc.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.jaxen.saxpath.Axis;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.boco.modules.fdoc.dao.ExpertAccountDao;
import com.boco.modules.fdoc.dao.ExpertInfoDao;
import com.boco.modules.fdoc.model.expert.ExpertAccountEntity;
import com.boco.modules.fdoc.model.expert.ExpertInformationEntity;
import com.boco.modules.fdoc.service.ExpertService;
import com.boco.modules.fdoc.vo.ExpertVo;

/**
 * Description
 * @author lzz
 * @date 2017年9月26日 上午9:40:21
 */
@Service
public class ExpertServiceImpl implements ExpertService {

	@Resource
	ExpertInfoDao infoDao;
	@Resource
	ExpertAccountDao accountDao;
	
	
	@Override
	public int getExpertCount(ExpertVo expertVo) {
		return infoDao.getExpertCount(expertVo);
	}
	
	@Override
	public List<ExpertVo> getExpertList(ExpertVo expertVo) {
		return infoDao.getExpertList(expertVo);
	}
	
	@Transactional
	@Override
	public int saveExpertAndAccount(ExpertInformationEntity informationEntity, ExpertAccountEntity accountEntity) {
		
		ExpertInformationEntity eEntity=infoDao.getExpertByIdCard(informationEntity.getIdCard());
		if(eEntity != null){
			return -1;
		}
		infoDao.insertInfo(informationEntity);
		int flag=accountDao.insertAccount(accountEntity);
		return flag;
	}
	
	@Override
	public ExpertVo getExpertById(String id) {
		return infoDao.getExpertById(id);
	}
	
	@Transactional
	@Override
	public int updateExpert(ExpertInformationEntity informationEntity, ExpertAccountEntity accountEntity) {
		ExpertInformationEntity eEntity=infoDao.getExpertByIdCard(informationEntity.getIdCard());
		if(eEntity != null && !eEntity.getId().equals(informationEntity.getId())){
			return -1;
		}
		infoDao.updateInfoById(informationEntity);
		int flag=1;
		ExpertAccountEntity aEntity=accountDao.getExpertAccount(accountEntity.getExpertId());
		if(accountEntity.getExpertAccountId()==null && aEntity==null){
			accountEntity.setExpertAccountId(UUID.randomUUID().toString().replace("-", ""));
			accountEntity.setUpdateTime(null);
			accountEntity.setCreateTime(new Date());
			flag=accountDao.insertAccount(accountEntity);
		}else if(accountEntity.getExpertAccountId()!=null && aEntity==null){
			flag=accountDao.updateAccountById(accountEntity);
		}else{
			flag=accountDao.updateAccountByinfo(accountEntity);
		}
		return flag;
	}
	
}
